<?php

namespace app\shop\model\company;
use think\model\concern\SoftDelete;
use app\shop\model\user\User;
use think\Model;
/**
 * 应用模型
 */
class TrainStudent extends Model
{
    use SoftDelete;
    protected $autoWriteTimestamp = true;

    protected $pk = "user_id";

    const STATION_ON = 0;       //培训中
    const STATION_DONE = 1;     //培训结束

    public function department()
    {
        return $this->belongsTo(Department::class, 'department_id', 'id');
    }

    public function station()
    {
        return $this->belongsTo(Station::class, 'station_id', 'id');
    }

    public function getStatusTextAttr($value, $data)
    {
        $status = $data['status'] ?? 0;
        switch ($status) {
            case self::STATION_ON:
                $status = '培训中';
                break;
            case self::STATION_DONE:
                $status = '培训结束';
                break;
            default:
                $status = '未知';
                break;
        }
        return $status;
    }

    public function staff()
    {
        return $this->belongsTo(Staff::class, 'user_id', 'user_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'user_id');
    }

    public function trainLog()
    {
        return $this->hasMany(TrainLog::class, 'user_id', 'user_id');
    }

    public function getProgressAttr()
    {
        return $this->trainLog()->order('id desc')->value('progress');
    }
}
